package com.enjoyor.soa.traffic.restt.tpcms.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.ITemplateConsumeService;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/userCheck" })
@Api("用户信息Controller相关api")
@CrossOrigin
public class UserCheckController {

    @Autowired
    private ITemplateConsumeService dubboTokenServiceInvoke;

    @Value("${sso.client.filter[systemKey]}")
    private String systemKey;

    @GetMapping(value = { "/selectUser" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "获取用户信息", notes = "校验token并获取用户信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", required = true, dataType = "string")
    public ResultPojo selectUser(@RequestParam(defaultValue = "") String token, HttpServletRequest request) {
        return new ResultPojo((UserDto) request.getAttribute("currentUser"));
    }

    @PostMapping(value = { "/checkToken" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    public ResultPojo checkToken(@RequestParam(defaultValue = "") String token) {
        return ResultHelper.getResult(dubboTokenServiceInvoke.check(token, systemKey));
    }

}
